class StudentsController < ApplicationController
	def create
		@user = User.new(params[:user])
		@user.role = USER_ROLE_STATUS[:student]
	    respond_to do |format|
	      if @user.save
	        flash[:notice] = 'User was successfully created.'
	        format.html { redirect_to(@user) }
	        format.xml  { render :xml => @user, :status => :created, :location => @user }
	      else
	        format.html { render :action => "new" }
	        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
	      end
	    end				
	end

	def index
		user = User.find(params[:user_id])
		questions = user.questions.collect{|question| 
			attrs = question.attributes
			attrs[:unread_count] = QaDetail.count(:conditions => [
				"is_read <> true AND question_id = ? AND user_id = ?",				
				question.id,
				user.id
			])
			attrs 
		}
		
		render :json => questions
	end

	def unread
		user = User.find(params[:user_id])
		render :json => {:count => user.questions.count{|question| question.status == QUESTION_STATUS[:answered]}}
	end
end
